package com.xiaobingby.auth.config;

import com.xiaobingby.auth.authentication.AuthenctiationFailureHandler;
import com.xiaobingby.auth.authentication.AuthenticationSuccessHandler;
import com.xiaobingby.auth.authentication.mobile.SmsCodeAuthenticationSecurityConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author XiaoBingBy
 * @date 2018-08-26 12:00
 * @since 1.0
 */
@Primary
@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {

    @Autowired
    private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig;

    @Autowired
    private AuthenticationSuccessHandler authenticationSuccessHandler;

    @Autowired
    private AuthenctiationFailureHandler authenctiationFailureHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        http.formLogin()
//                .successHandler(authenticationSuccessHandler)
//                .failureHandler(authenctiationFailureHandler)
//                .and()
//                .apply(smsCodeAuthenticationSecurityConfig)
//                .and()
//                .authorizeRequests()
//                .antMatchers("/actuator/**", "/authentication/mobile", "/imageCode/**").permitAll()
//                .anyRequest()
//                .authenticated()
//                .and()
//                .csrf().disable();

        http.authorizeRequests()
                .antMatchers("/actuator/**", "/imageCode/**").permitAll()
                .anyRequest().authenticated()
                .and().csrf().disable();

        //http.exceptionHandling().authenticationEntryPoint(new MyAuthenticationEntryPoint());
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        AuthenticationManager authenticationManager = super.authenticationManagerBean();
        return authenticationManager;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

    /**
     * 错误自定义处理
     */
    private class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {

        @Override
        public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
            System.out.println(authException);
        }

    }

}
